var dragMoveX = 0, imgDrager = '';
(function($){
    $.fn.drag = function(options, sucfun, errfun){
        var isvalid = false;
        var x, drag = this, isMove = false, defaults = {
        };
        imgDrager = this;
        var options = $.extend(defaults, options);
        //添加背景，文字，滑块
        var html = '<div class="drag_bg"></div>'+
            '<div class="drag_text" onselectstart="return false;" unselectable="on">拖动滑块拼图完成验证</div>'+
            '<div class="handler handler_bg"></div>';
        this.append(html);
        
        var handler = drag.find('.handler');
        var drag_bg = drag.find('.drag_bg');
        var text = drag.find('.drag_text');
        var maxWidth = drag.width() - handler.width();  //能滑动的最大间距

        //鼠标按下时候的x轴的位置
        handler.mousedown(function(e){
            if(isvalid) {
                return false;
            }
            verifyImgSource.css("display", "none");
            verifyImgCut.show();
            verifyImgDrag.show();
            isMove = true;
            x = e.pageX - parseInt(handler.css('left'), 10);
        });
        var verifyImgDrag = $("#verifyImgDrag");
        var verifyImgSource = $("#verifyImgSource");
        var verifyImgCut = $("#verifyImgCut");
        //鼠标指针在上下文移动时，移动距离大于0小于最大间距，滑块x轴位置等于鼠标移动距离
        $("#drag").mousemove(function(e){
            if(isvalid) {
                return false;
            }
            var _x = e.pageX - x;
            dragMoveX = _x;
            if(isMove){
                if(_x > 0 && _x <= maxWidth){
                	verifyImgDrag.css({'left': _x});
                    handler.css({'left': _x});
                    drag_bg.css({'width': _x});
                }else if(_x > maxWidth){  //鼠标指针移动距离达到最大时清空事件
                  //  dragOk();
                }
            }
        }).mouseup(function(e){
            if (isvalid || x == null || x == '' || typeof x == 'undefined') {
                return false;
            }
            isMove = false;
            var _x = e.pageX - x;
            $.ajax({
                type:"POST",
                url: "/index/checkImgVerify",
                dataType:"JSON",
                async: false,
                data:{point:_x},
                success:function(result){
                    if(result == "-1" || result == -4){
                        initVerificationCode();
                        $('#verifyImgDrag').css('left', 0);
                        $('#drag').empty().css({'background-color': '#e8e8e8', 'color': ''});
                        $('#drag').drag(null, null, initVerificationCode);
                    }else if(result == "success") {
                        dragOk(_x);
                    }else{
                        dragErr();
                    }
                },
                error: function(err) {
                	alert('校验验证码时出错');
                    dragErr();
                }
            });
        });

        var errcount = 0;
        function dragErr() {
            errcount = errcount + 1;
            isvalid = false;
            text.text("验证失败");
            handler.removeClass('handler_bg').addClass('handler_err_bg');
            setTimeout(function(){
                //还原
                text.text("拖动滑块拼图完成验证");
                handler.removeClass("handler_err_bg").addClass("handler_bg");
                verifyImgDrag.css({'left': -10});
                handler.css({'left': 0});
                drag_bg.css({'width': 0});
                //验证失败, 拖动错误数大于3次，那么就重置
                if(errcount >= 3) {
                    errfun();
                    errcount = 0;
                    verifyImgDrag.css("display", "none");
                    verifyImgSource.show();
                    verifyImgCut.css("display", "none");
                }
            }, 1000);
        }
        //清空事件
        function dragOk(_x){
            isvalid = true;
            handler.removeClass('handler_bg').addClass('handler_ok_bg');
            text.text('验证通过');
            drag.css({'color': '#fff', 'background-color': '#7ac23c'});
            handler.unbind('mousedown');
            drag.unbind('mousemove mouseup');
            $('#verifyImgSource,#verifyImgCut').unbind('click');
            $('#verifyImgSource,#verifyImgCut').css({'cursor': 'default'});
            $("#isPassVerify").val("S");
            var errorMsg = I('errorPrompt').innerText;
            if(errorMsg == '请拖动滑块完成验证'){
            	$('#errorPrompt').text('');
            }
            //显示原始图片,并做光线闪过
            verifyImgDrag.css("display", "none");
            verifyImgCut.css("display", "none");
            verifyImgSource.show();
            verifyImgSource.addClass("run");
        }
    };
})(jQuery);